﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hamaazan.Data;

namespace Hamaazan.Logic
{
   public class Users
    {
       private static Users m_Instance = null;
        private static object _synclock = new object();

        public static Users Instance
        {
            get
            {
                lock (_synclock)
                {
                    if (m_Instance == null)
                    {
                        m_Instance = new Users();
                    }
                }
                return m_Instance;
            }
        }


       public User UserCheck(string email, string jsonObject, string jsonObjectType)
        {
            using (HamaazanEntities db = new HamaazanEntities())
            {
                var result = db.Users.Where(o => o.Email == email).SingleOrDefault();
                if (result == null)
                {
                    User user = new User();
                    user.Id = Guid.NewGuid();
                    user.Email = email;
                    user.JsonObject = jsonObject;
                    user.JsonObjectType = jsonObjectType;
                    db.Users.AddObject(user);
                    db.SaveChanges();
                    result = user;
                }
                else
                {
                    result.Email = email;
                    result.JsonObject = jsonObject;
                    result.JsonObjectType = jsonObjectType;
                    db.SaveChanges();
                }
                return result;

            }

        }
    }
}
